package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.exception.BusinessException;
import com.ym.core.utils.tkmybatis.TK;

import com.ym.medical.dao.PhoneRuleDao;
import com.ym.medical.dao.PrivateDoctorRuleDao;
import com.ym.medical.dao.ResultDoctorDao;
import com.ym.medical.dao.TextRuleDao;
import com.ym.medical.domain.entity.PhoneRuleEntity;
import com.ym.medical.domain.entity.TextRuleEntity;
import com.ym.medical.domain.param.ExamDoctorParam;
import com.ym.medical.domain.param.PhoneRuleModel;
import com.ym.medical.domain.param.TextRuleParam;
import com.ym.medical.domain.vo.*;
import com.ym.medical.enums.DoctorServiceItemEnum;
import com.ym.medical.service.IConsultSV;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ConsultSVImpl  implements IConsultSV {

	@Autowired
	private TextRuleDao textRuleDao;
	@Autowired
	private PhoneRuleDao phoneRuleDao;
	@Autowired
	private PrivateDoctorRuleDao privateDoctorRuleDao;
	@Autowired
	private ResultDoctorDao resultDoctorDao;

	/**
	 * 图文问诊服务次数配置增加
	 *
	 * @param textRule
	 * @return
	 */
	@Override
	public TextRuleEntity insertTextRule(TextRuleEntity textRule) {
		textRuleDao.insertSelective(textRule);
		return textRule;
	}

	/**
	 * 图文问诊服务次数配置修改
	 *
	 * @param textRule
	 * @return
	 */
	@Override
	public TextRuleEntity updateTextRule(TextRuleEntity textRule) {
		if (selectTextRuleById(textRule.getId()) == null) {
			throw new BusinessException("该图文问诊不存在！");
		}
		textRuleDao.updateByPrimaryKeySelective(textRule);
		return textRule;
	}

	/**
	 * 图文问诊服务次数配置删除
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public void deleteTextRule(Long[] ids) {
		textRuleDao.deleteByExample(TK.selectWhere(TextRuleEntity.class)
				.andIn(TextRuleEntity::getId,ids).end());
	}

	/**
	 * 电话问诊新增
	 *
	 * @param phoneRule
	 * @return
	 */
	@Override
	public PhoneRuleEntity insertPhoneRule(PhoneRuleEntity phoneRule) {
		phoneRuleDao.insertSelective(phoneRule);
		return phoneRule;
	}

	/**
	 * 电话问诊修改
	 *
	 * @param phoneRule
	 * @return
	 */
	@Override
	public PhoneRuleEntity updatePhoneRule(PhoneRuleEntity phoneRule) {
		if (selectPhoneRuleById(phoneRule.getId()) == null) {
			throw new BusinessException("该电话问诊不存在！");
		}
		phoneRuleDao.updateByPrimaryKeySelective(phoneRule);
		return phoneRule;
	}

	/**
	 * 电话问诊删除
	 *
	 * @param ids
	 * @return
	 */
	@Override
	public void deletePhoneRule(Long[] ids) {
		phoneRuleDao.deleteByExample(TK.selectWhere(PhoneRuleEntity.class)
		.andIn(PhoneRuleEntity::getId,ids).end());
	}

	/**
	 * 图文问诊服务次数配置展示
	 *
	 * @param textRule
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo<TextRuleEntity> listTextRule(TextRuleParam textRule, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<TextRuleEntity> textRules = textRuleDao.select(textRule.covert());
		return new PageInfo<>(textRules);
	}

	/**
	 * 电话问诊服务时间配置展示
	 *
	 * @param phoneRule
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public PageInfo<PhoneRuleEntity> listPhoneRule(PhoneRuleModel phoneRule, int pageNum, int pageSize) {
		List<PhoneRuleEntity> phoneRules = phoneRuleDao.select(phoneRule.covert());
		return new PageInfo<>(phoneRules);
	}

	/**
	 * 查询医生的服务项
	 *
	 * @param doctorId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public List<DoctorServiceVO> serviceItemList(Long doctorId, String seriLabel, int pageNum, int pageSize) {
		ArrayList<DoctorServiceVO> doctorServiceVOS = new ArrayList<>();
		/* 查询医生图文服务项*/
		if (DoctorServiceItemEnum.BY_INQUIRING.getKey().equals(seriLabel)) {
			List<TextRuleVo> textRuleModel = textRuleDao.selectAllTextItem(doctorId);
			if (!textRuleModel.isEmpty()) {
				for (TextRuleVo text : textRuleModel) {
					DoctorServiceVO doctorServiceVO = new DoctorServiceVO();
					doctorServiceVO.setServiceNum(text.getTrRound());
					doctorServiceVO.setServicePrice(text.getDspPrice());
					doctorServiceVOS.add(doctorServiceVO);
				}
			}
		}
		/*查询医生电话服务项*/
		if (DoctorServiceItemEnum.CALL_PHYSICIAN_VISITS.getKey().equals(seriLabel)) {
			List<PhoneRuleModel> phoneRuleModel = phoneRuleDao.selectAllPhoneItem(doctorId);
			if (!phoneRuleModel.isEmpty()) {
				for (PhoneRuleModel phone : phoneRuleModel) {
					DoctorServiceVO doctorServiceVO = new DoctorServiceVO();
					doctorServiceVO.setServicePrice(phone.getDspPrice());
					doctorServiceVO.setServiceTime(phone.getPrDuration());
					doctorServiceVOS.add(doctorServiceVO);
				}
			}
		}

		/*查询医生私人服务项*/
		if (DoctorServiceItemEnum.PRIVATE_DOCTORS.getKey().equals(seriLabel)) {
			List<PrivateDoctorRuleVo> privateDoctorRuleModel = privateDoctorRuleDao.selectAllPrivateDoctorRule(doctorId);
			if (privateDoctorRuleModel.size() > 0) {
				for (PrivateDoctorRuleVo privateDoctor : privateDoctorRuleModel) {
					DoctorServiceVO doctorServiceVO = new DoctorServiceVO();
					doctorServiceVO.setServicePrice(privateDoctor.getDspPrice());
					doctorServiceVO.setServiceTime(privateDoctor.getPdrDays());
					doctorServiceVO.setServiceNum(privateDoctor.getPdrCount());
					doctorServiceVOS.add(doctorServiceVO);
				}
			}
		}
		return doctorServiceVOS;
	}

	/**
	 * 查询医生服务项
	 *
	 * @param userId
	 * @return
	 */
	public DoctorServiceItemsVo selectserviceItem(Long userId) {
		DoctorServiceItemsVo doctorServiceItemsVo = new DoctorServiceItemsVo();

		return doctorServiceItemsVo;
	}

	/**
	 * 医生咨询服务管理展示
	 *
	 * @param examDoctor
	 * @return
	 */
	@Override
	public PageInfo<DoctorServiceItemVO> doctorList(ExamDoctorParam examDoctor, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<>(resultDoctorDao.selectByExamDoctorServiceItemVO(examDoctor));
	}

	private PhoneRuleEntity selectPhoneRuleById(Long id) {
		return phoneRuleDao.selectByPrimaryKey(id);
	}

	private TextRuleEntity selectTextRuleById(Long id) {
		return textRuleDao.selectByPrimaryKey(id);
	}
}
